type filterPattern = 'save' | 'remove';

//对对象进行过滤
export function filterObjectField(
  resource: object,
  propertyArr: string[],
  pattern: filterPattern,
): any {
  if (pattern === 'save') {
    //保留字段
    const copyObject = {};
    for (const property of propertyArr) {
      if (property in resource) copyObject[property] = resource[property];
    }
    return copyObject;
  } else if (pattern === 'remove') {
    for (const property of propertyArr) {
      resource[property] = undefined;
    }
    return resource;
  }
}

export const changeObjectField = (function () {
  //改变对象的字段，数据不变
  function changeField(resource, oldField, newField) {
    const value = resource[oldField];
    resource[newField] = value;
    resource[oldField] = undefined;
  }
  //处理参数
  function changeObjectField(resource, fieldMap) {
    if (typeof fieldMap != 'object') return; //不是对象
    const fieldMapType = Array.isArray(fieldMap);
    if (fieldMapType) {
      //数组
      const oldField = fieldMap[0];
      const newField = fieldMap[1];
      changeField(resource, oldField, newField);
    } else {
      //对象
      for (const pro in fieldMap) {
        const oldField = pro;
        const newField = fieldMap[pro];
        changeField(resource, oldField, newField);
      }
    }
    return resource;
  }
  return changeObjectField;
})();

//求第一个数组中，不存在于第二个数组中
export function filterArr(firstArr, secondArr) {
  return firstArr.filter((value) => {
    const targetindex = secondArr.indexOf(value);
    if (targetindex === -1) {
      return value;
    }
  });
}
